package com.sctvcloud.renhe.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.barrage.BarrageListView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.renhe.R;

/**
 * Created by liuyu on 2017/9/17.
 */

public class BarragePlayerView extends BasePlayerView {

    //    protected BarrageListView barrageView;
    private RelativeLayout commentLayout;
    private CustomFontTextView commentInput;


    private boolean hasBarrage = true;//用于设置是否显示弹幕开关键（此版本暂时不进行处理）

    protected boolean isBarrageOpen;//弹幕是否打开

    public BarragePlayerView(Context context, Boolean fullFlag) {
        super(context, fullFlag);
    }

    public BarragePlayerView(Context context) {
        super(context);
    }

    public BarragePlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void init(Context context) {
        super.init(context);
//        barrageView = (BarrageListView) findViewById(R.id.player_barrage);
        vBarrageSwitch = (TextView) findViewById(R.id.player_barrage_switch);
        commentLayout = (RelativeLayout) findViewById(R.id.layout_bottom_comment);
        commentInput = (CustomFontTextView) findViewById(R.id.comment_txt);
        commentLayout.setVisibility(View.GONE);
        vBarrageSwitch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isBarrageOpen) {
                    isBarrageOpen = false;
                    vBarrageSwitch.setText(R.string.show_barrage);
                    if (null != barrageView) {
                        barrageView.hideRecycler();
                    }
                } else {
                    isBarrageOpen = true;
                    vBarrageSwitch.setText(R.string.close_barrage);
                    if (null != barrageView) {
                        barrageView.showRecycler();
                    }
                }
            }
        });
        commentInput.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != sendListener) {
                    sendListener.onSendComment();
                }
            }
        });
//        Cache.getInstance().setFirstInitLiving(false);
    }


    @Override
    protected void onClickFullBtn() {
        super.onClickFullBtn();
        //切换全屏与非全屏，清空评论显示
        barrageView.clearAllMsg();
    }

    @Override
    public int getLayoutId() {
        return R.layout.video_layout_barrage;
    }

    @Override
    protected void changeUiToNormal() {
        super.changeUiToNormal();
        JLog.e("ccsc  " + mThumbImageView);
    }

    @Override
    public BarragePlayerView startWindowFullscreen(Context context, boolean actionBar, boolean statusBar) {
        BarragePlayerView bpv = (BarragePlayerView) super.startWindowFullscreen(context, actionBar, statusBar);
        bpv.setBarrageOpen(true);
        bpv.setHasBarrage(true);

        return bpv;
    }

    @Override
    protected void exchangeFullScreen(boolean isChangedFull) {
        super.exchangeFullScreen(isChangedFull);
        if (isChangedFull) {
            if (null != barrageView) {
                this.addView(barrageView);
            }
            //非第一次加载时，显示评论输入
            commentLayout.setVisibility(View.VISIBLE);
        } else {
            commentLayout.setVisibility(View.GONE);
        }
    }

    public void addMsg(String msg) {
        BarragePlayerView bpv = (BarragePlayerView) getFullWindowPlayer();
//        JLog.e("ppiv  bpv="+bpv);
        if (bpv != null && bpv != this) {
            bpv.addMsg(msg);
        } else {
            if (barrageView != null) {
                barrageView.addData(msg);
            }
        }
    }

    public boolean canBarrage() {
        BarragePlayerView bpv = (BarragePlayerView) getFullWindowPlayer();
        if (bpv != null && bpv != this) {
            return bpv.canBarrage();
        }
        return hasBarrage && isBarrageOpen;
    }

    public void setHasBarrage(boolean hasBarrage) {
        this.hasBarrage = hasBarrage;
    }

    public void setBarrageOpen(boolean barrageOpen) {
        isBarrageOpen = barrageOpen;
    }

}
